<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: jinzhao
 * Date: 16-8-10
 * Time: 下午5:24
 */

class Login extends CI_Controller{
    public function index()
    {
        $this->load->helper('form');
        $this->load->view('login.html');
    }
    
    public function login_result()
    {
        $this->load->library('form_validation');
        $this->load->helper('form');
        
        $result = $this->form_validation->run('login');
        if (!$result){
            $this->load->view('login.html');
            return;
        }
        $email = $this->input->post('email');
        $password = $this->input->post('password');
        $remember_me = $this->input->post('remember_me');
       
        $login = array(
            'email' => $email,
            'password' => $password,
            'remember_me' => $remember_me
        );
        $this->load->model('user_model');
        $result = $this->user_model->login($login);
        if (!$result){
            if ($this->user_model->err_code === $this->user_model->ERROR_NOT_ACTIVE){
                $this->session->set_userdata('email', $email);
                redirect('account/active'); 
            }
            
            $data = array(
              'errmsg' => $this->user_model->display_errors()  
            );
            $this->load->view('login.html', $data);
            return;
        }
        $this->load->model('template_model');
        $this->template_model->init_count();
        
        $this->load->model('address_model');
        $this->address_model->init_count();
        redirect('home');
    }
    
    public function logout()
    {
        $this->session->unset_userdata('email');
        $this->session->unset_userdata('rand');
        redirect('login'); 
    }
}